package com.msmall.community.album.action.html;

import java.io.File;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.msmall.base.util.ConstantAjax;
import com.msmall.base.util.ConstantDB;
import com.msmall.community.album.model.Album;
import com.msmall.community.album.model.Photo;
import com.msmall.community.album.model.Reply;
import com.msmall.community.album.vo.ReplyVO;

public class PhotoAction extends AlbumAction{

	private static final long serialVersionUID = -1936710450518300590L;

	private static final Log logger = LogFactory.getLog(PhotoAction.class);
	
	private Long parentid;
	private Long replyid;
	private String content;

	protected Photo photo;
	private Reply reply;
	
	private Short enabledReply;
	private File[] images;
	private String[] imagesContentType;
	private String[] imagesFileName;
	
	private List<ReplyVO> replys;
	
	/**
	 * 进入上传照片页面
	 * @return
	 */
	public String create(){
		try {
			albums = albumManager.getAlumsByUserid(getUserid());
		} catch (Exception e) {
			logger.debug(e);
		}
		return SUCCESS;
	}
	/**
	 * 保存上传的照片
	 * @return
	 */
	public String save(){
		StringBuffer buffer = new StringBuffer();
		buffer.append(ConstantAjax.RETURN_PRE);
		try {
			if(enabledReply == null)
				enabledReply = ConstantDB.UNREPLY;
			albumManager.uploadImages(images, imagesFileName, albumid, getUserid(), enabledReply, getUserAgentType());
			buffer.append(createResult("true"))
			.append(ConstantAjax.CONTENT_PRE)
			.append(albumid)
			.append(ConstantAjax.CONTENT_SUFF);
		} catch (Exception e) {
			buffer.append(createResult("false"));
			logger.debug(e);
		}
		buffer.append(ConstantAjax.RETURN_SUFF);
		message = buffer.toString();
		return SUCCESS;
	}
	
	/**
	 * 删除照片
	 * @return
	 */
	public String remove(){
		return SUCCESS;
	}
	
	/**
	 * 查看图片
	 * @return
	 */
	public String view(){
		try{
			if(direct == null || direct == ConstantDB.DIRECTION_CURRENT ){
				photo = albumManager.getPhotoByid(photoid);
			}else if(direct == ConstantDB.DIRECTION_PREVIOUS){
				photo = albumManager.getPhotoPreByid(photoid);
			}else if(direct == ConstantDB.DIRECTION_NEXT){
				photo = albumManager.getPhotoNextByid(photoid);
			}
			album = (Album) albumManager.get(Album.class, photo.getAlbumid());
			
			replys = albumManager.getReplysByPhotoid(photoid, ConstantDB.AVALIABLE);
			if(album.getUserid().compareTo(getUserid()) == 0){
				isOwner = true;
			}else{
				isOwner = false;
			}
		}catch(Exception e){
			logger.error(e);
		}
		if(isOwner){
			return "photoViewOwner";
		}else{
			return "photoViewViewer";
		}
	}
	/**
	 * 进入创建回复页面
	 */
	public String reply(){
		return SUCCESS;
	}
	/**
	 * 保存回复
	 */
	public String saveReply(){
		try{
			boolean success = true;
			StringBuffer buffer = new StringBuffer();
			buffer.append(ConstantAjax.RETURN_PRE);
			if(!getIsLogin()){
				buffer.append(createResult("false"));
				buffer.append(createEasyContent(getText("common.authentication.anonymous")));
				success = false;
			}
			if(content == null && success){
				buffer.append(createResult("false"));
				buffer.append(createEasyContent(getText("html.klg.question.answer.create.content.length.short")));
				success = false;
			}
			if(success){
				int valid = albumManager.validateReplyContent(content);
				if(valid == 2){
					buffer.append(createResult("false"));
					buffer.append(createEasyContent(getText("html.klg.question.answer.create.content.length.short")));
					success = false;
				}
				if(valid == 3){
					buffer.append(createResult("false"));
					buffer.append(createEasyContent(getText("html.klg.question.answer.create.content.length.long")));
					success = false;
				}
				if(valid == 4){
					buffer.append(createResult("false"));
					buffer.append(createEasyContent(getText("html.klg.question.answer.create.content.illegal")));
					success = false;
				}
			}
			if(success){
				reply = new Reply();
				reply.setContent(content);
				reply.setParentid(parentid);
				reply.setPhotoid(photoid);
				reply = albumManager.savePhotoReply(reply, getUserid(), getUserAgentType());
				buffer.append(createResult("true"));
			}
			buffer.append(ConstantAjax.RETURN_SUFF);
			message = buffer.toString();
		}catch(Exception e){
			log.debug("============>", e);
		}
		return SUCCESS;
	}
	/**
	 * 删除回复
	 */
	public String removeReply(){
		return SUCCESS;
	}
	
	public Photo getPhoto() {
		return photo;
	}
	public void setPhoto(Photo photo) {
		this.photo = photo;
	}
	public Long getReplyid() {
		return replyid;
	}
	public void setReplyid(Long replyid) {
		this.replyid = replyid;
	}
	public String getContent() {
		return content;
	}
	public void setContent(String content) {
		this.content = content;
	}
	public Reply getReply() {
		return reply;
	}
	public void setReply(Reply reply) {
		this.reply = reply;
	}
	public Short getEnabledReply() {
		return enabledReply;
	}
	public void setEnabledReply(Short enabledReply) {
		this.enabledReply = enabledReply;
	}
	public File[] getImages() {
		return images;
	}
	public void setImages(File[] images) {
		this.images = images;
	}
	public String[] getImagesContentType() {
		return imagesContentType;
	}
	public void setImagesContentType(String[] imagesContentType) {
		this.imagesContentType = imagesContentType;
	}
	public String[] getImagesFileName() {
		return imagesFileName;
	}
	public void setImagesFileName(String[] imagesFileName) {
		this.imagesFileName = imagesFileName;
	}
	public List<ReplyVO> getReplys() {
		return replys;
	}
	public void setReplys(List<ReplyVO> replys) {
		this.replys = replys;
	}
}
